#include <iostream>
#include <string>
using namespace std;

const int kMaxN = 20;
const int kInf = 1e9;

int f[kMaxN][2];
int ll[kMaxN], rr[kMaxN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;

    cin >> s;
    ll[i] = m + 1;
    rr[i] = 0;
    for (int j = 0; j <= m + 1; j++) {
      if (s[j] == '1') {
        ll[i] = min(ll[i], j);
        rr[i] = max(rr[i], j);
      }
    }
    if (rr[i] == 0 && i == 0) {
      n--;
      i--;
    }
  }
  if (n > 0) {
    f[n - 1][0] = 0;
    f[n - 1][1] = kInf;
    for (int i = n - 1; i > 0; i--) {
      f[i - 1][0] = min(f[i][0] + 2 * rr[i], f[i][1] + m + 1) + 1;
      f[i - 1][1] = min(f[i][1] + 2 * (m + 1 - ll[i]), f[i][0] + m + 1) + 1;
    }
    cout << min(f[0][0] + rr[0], f[0][1] + (m + 1 - ll[0])) << "\n";
  } else {
    cout << "0\n";
  }
  return 0;
}